<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="相对路径" prop="path">
        <el-input
            v-model="queryParams.path"
            placeholder="请输入相对路径"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件名" prop="name">
        <el-input
            v-model="queryParams.name"
            placeholder="请输入文件名"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="后缀" prop="suffix">
        <el-input
            v-model="queryParams.suffix"
            placeholder="请输入后缀"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="大小|字节B" prop="size">
        <el-input
            v-model="queryParams.size"
            placeholder="请输入大小|字节B"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已上传分片" prop="shardIndex">
        <el-input
            v-model="queryParams.shardIndex"
            placeholder="请输入已上传分片"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分片大小|B" prop="shardSize">
        <el-input
            v-model="queryParams.shardSize"
            placeholder="请输入分片大小|B"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分片总数" prop="shardTotal">
        <el-input
            v-model="queryParams.shardTotal"
            placeholder="请输入分片总数"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件标识" prop="fileMd">
        <el-input
            v-model="queryParams.fileMd"
            placeholder="请输入文件标识"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="租户编号" prop="tenantId">
        <el-input
            v-model="queryParams.tenantId"
            placeholder="请输入租户编号"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">清空</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['group:shard:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['group:shard:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['group:shard:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['group:shard:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="主键id" align="center" prop="id"/>
      <el-table-column label="相对路径" align="center" prop="path"/>
      <el-table-column label="文件名" align="center" prop="name"/>
      <el-table-column label="后缀" align="center" prop="suffix"/>
      <el-table-column label="大小|字节B" align="center" prop="size"/>
      <el-table-column label="已上传分片" align="center" prop="shardIndex"/>
      <el-table-column label="分片大小|B" align="center" prop="shardSize"/>
      <el-table-column label="分片总数" align="center" prop="shardTotal"/>
      <el-table-column label="文件标识" align="center" prop="fileMd"/>
      <el-table-column label="租户编号" align="center" prop="tenantId"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
              size="mini"
              type="text"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['group:shard:edit']"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="Delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['group:shard:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum"
                v-model:limit="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改文件对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="相对路径" prop="path">
          <el-input v-model="form.path" placeholder="请输入相对路径"/>
        </el-form-item>
        <el-form-item label="文件名" prop="name">
          <el-input v-model="form.name" placeholder="请输入文件名"/>
        </el-form-item>
        <el-form-item label="后缀" prop="suffix">
          <el-input v-model="form.suffix" placeholder="请输入后缀"/>
        </el-form-item>
        <el-form-item label="大小|字节B" prop="size">
          <el-input v-model="form.size" placeholder="请输入大小|字节B"/>
        </el-form-item>
        <el-form-item label="已上传分片" prop="shardIndex">
          <el-input v-model="form.shardIndex" placeholder="请输入已上传分片"/>
        </el-form-item>
        <el-form-item label="分片大小|B" prop="shardSize">
          <el-input v-model="form.shardSize" placeholder="请输入分片大小|B"/>
        </el-form-item>
        <el-form-item label="分片总数" prop="shardTotal">
          <el-input v-model="form.shardTotal" placeholder="请输入分片总数"/>
        </el-form-item>
        <el-form-item label="文件标识" prop="fileMd">
          <el-input v-model="form.fileMd" placeholder="请输入文件标识"/>
        </el-form-item>
        <el-form-item label="是否删除：0正常，-1删除" prop="deleted">
          <el-input v-model="form.deleted" placeholder="请输入是否删除：0正常，-1删除"/>
        </el-form-item>
        <el-form-item label="租户编号" prop="tenantId">
          <el-input v-model="form.tenantId" placeholder="请输入租户编号"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as shardServer from "@/api/group/shard";

export default {
  name: "Shard",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件表格数据
      shardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        path: null,
        name: null,
        suffix: null,
        size: null,
        shardIndex: null,
        shardSize: null,
        shardTotal: null,
        fileMd: null,
        tenantId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        path: [
          {required: true, message: "相对路径不能为空", trigger: "blur"}
        ],
        createTime: [
          {required: true, message: "创建时间不能为空", trigger: "blur"}
        ],
        updateTime: [
          {required: true, message: "更新时间不能为空", trigger: "blur"}
        ],
        deleted: [
          {required: true, message: "是否删除：0正常，-1删除不能为空", trigger: "blur"}
        ],
        tenantId: [
          {required: true, message: "租户编号不能为空", trigger: "blur"}
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询文件列表 */
    getList() {
      this.loading = true;
      shardServer.listShard(this.queryParams).then(response => {
        this.shardList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单清空
    reset() {
      this.form = {
        id: null,
        path: null,
        name: null,
        suffix: null,
        size: null,
        shardIndex: null,
        shardSize: null,
        shardTotal: null,
        fileMd: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deleted: null,
        tenantId: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 清空按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      shardServer.getShard({id: id}).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文件";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            shardServer.updateShard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            shardServer.addShard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id ? [row.id] : ids.value;
      this.$modal.confirm('是否确认删除文件编号为"' + ids + '"的数据项？').then(function () {
        return shardServer.delShard({delIds: _ids});
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('group/shard/export', {
        ...this.queryParams
      }, `shard_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
